package com.hs.srm.admin.modules.ssess.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import sunnykid.text.SunnykidText;

import com.hs.srm.admin.modules.orders.entity.StockEntity;
import com.hs.srm.admin.modules.orders.service.StockService;
import com.hs.srm.admin.modules.pricemanagement.entity.EssentialInformationEntity;
import com.hs.srm.admin.modules.pricemanagement.service.EssentialInformationService;
import com.hs.srm.admin.modules.requisition.entity.NproPurchRequestItemEntity;
import com.hs.srm.admin.modules.requisition.service.NproPurchRequestItemService;
import com.hs.srm.admin.modules.requisition.service.ProPurchRequestItemService;
import com.hs.srm.admin.modules.ssess.entity.SsessEntity;
import com.hs.srm.admin.modules.ssess.service.SsessService;
import com.hs.srm.admin.modules.ssess.vo.SsessAndProPurchRequest;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.admin.modules.vendor.service.VendorService;
import com.hs.srm.base.common.annotation.SysLog;
import com.hs.srm.common.utils.DateUtils;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;
import com.hs.srm.common.validator.ValidatorUtils;
import com.hs.srm.common.validator.group.UpdateGroup;

/**
 * @author yanfei
 *
 * @date 2019年3月25日
 * 月度评分
 */
@RestController
@RequestMapping("/ssess")
public class SsessController extends AbstractController {
	private static final long serialVersionUID = -4123823577744890422L;
	@Autowired
	private SsessService ssessService;
	@Autowired
	private NproPurchRequestItemService nproPurchRequestItemService;
	@Autowired
	private ProPurchRequestItemService proPurchRequestItemService;
	@Autowired
	private StockService stockService;
	@Autowired
	private EssentialInformationService essentialInformationServiceImpl;
	
	@Autowired
	private VendorService vendorService;
	
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = ssessService.queryPage(params);
		return R.ok(page);
	}
	@SysLog("保存月度评分的信息")
	@RequestMapping(value = "/saveBasic")
	@RequiresPermissions("vendor:save")
	public R saveBasic(SsessEntity ssessEntity) {
		if(ssessEntity != null) {
			this.ssessService.insert(ssessEntity);
			return R.ok();
		}
		return R.error();
	}
	/**   
	 * 跳转至供应商月度编辑页面
	 * @param userId
	 * @return
	 */
	@RequestMapping("/toEdit/{markId}")
	public ModelAndView toEdit(@PathVariable("markId") String markId) {
		SsessEntity ssessEntity = this.ssessService.selectById(markId);

		// 把查询出来的对象赋值给供应商实体
		ModelAndView modelAndView = new ModelAndView("modules/ssess/ssess_edit");
		modelAndView.addObject("ssessEntity", ssessEntity);
		return modelAndView;
	}
	
	/**
	 * 更新月度评估信息
	 */
	@SysLog("更新月度评估信息 ")
	@RequestMapping("/update")
	public R update(SsessEntity ssessEntity) {
		BigDecimal count = new BigDecimal(0);
		ValidatorUtils.validateEntity(ssessEntity, UpdateGroup.class);
		if(ssessEntity!=null){
			if(ssessEntity.getQualityMark()==0){
				ssessEntity.setQualityMark(Double.valueOf("0"));
				ssessEntity.setAverage(new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP)); //平均分
			}
		    if(ssessEntity.getQualityMark()!=0){
		    	//总分
		    	count = count.add(new BigDecimal(ssessEntity.getPriceMark()))//价格评分
		    			     .add(new BigDecimal(ssessEntity.getDeliveryTimeMark()))//交货评分
		    	             .add(new BigDecimal(ssessEntity.getPriceMark()))//服务评分
		    	             .add(new BigDecimal(ssessEntity.getQualityMark()));//品质评分
		    	//.setScale(2, BigDecimal.ROUND_HALF_UP)
		    	ssessEntity.setAverage(count.divide(new BigDecimal(4)).setScale(2, BigDecimal.ROUND_HALF_UP));
		    	ssessEntity.setLastUpdateDate(new Date());
		    	ssessEntity.setLastUpdatedBy(this.getUser().getName());
		    }
			this.ssessService.updateById(ssessEntity);
		}
		return R.ok();
	}
	/**
	 * 计算评估分数 (非生产进行评分)
	 */  
	//@Scheduled(cron = "0/5 * * * * ?")
	public void addAScore() {
			        //这里的定时任务的代码执行块
					double JQ_GRADE = 0; //交期最终得分；
					double JH_NUM_GRADE = 0; //交货数量
					double JG_GRADE = 0; //价格分数
					List<NproPurchRequestItemEntity> list = nproPurchRequestItemService.selectRequestItem();
					String purchRequestCodetem =""; //采购申请临时编码
					int applyQty=0;        //订单数量
					 // 不同类型转为BigDecimal
					BigDecimal maapplyQty = new BigDecimal(applyQty);
					SsessEntity ssessEntity = new SsessEntity();
					double flat = 0;
					for(NproPurchRequestItemEntity nproPurchRequestItemEntity :list){
						Date date = nproPurchRequestItemEntity.getDeliveryDate(); //得到交货日期
						String purchRequestCode =nproPurchRequestItemEntity.getPurchRequestCode(); //采购申请编码
						//获取订单号
						String poNo = nproPurchRequestItemEntity.getPoNo();
						int shippedQty = 0;
						BigDecimal mashippedQty = new BigDecimal(shippedQty);
						List<StockEntity> listStock = stockService.selectStockByDdh(poNo); //获取期内实际交货量
						if(listStock.size()>0){
							for(StockEntity stock : listStock){
								mashippedQty = mashippedQty.add(stock.getShippedQty());
							}
						    if(null!=purchRequestCodetem&&!purchRequestCodetem.equals(purchRequestCode)){
									purchRequestCodetem = purchRequestCode;
							}
							//防止出现空指针问题
							if(null!=purchRequestCodetem&&purchRequestCodetem.equals(purchRequestCode)){
								maapplyQty = maapplyQty.add(nproPurchRequestItemEntity.getApplyQty());//订单申请总数量
							}
						}
						java.util.Date deliveryDate = DateUtils.stringToDate(DateUtils.format(date, DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN);	//格式货交货日期
						java.util.Date newDate = DateUtils.stringToDate(DateUtils.format(DateUtils.currentDateTime(), DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN); //系统时间格式话
						int days=0;
						if(null!=deliveryDate){
							days =  DateUtils.differentDaysByMillisecond(newDate, deliveryDate); //交期减去系统日期
						}
						//得到交货日期和系统时间相差几天
						//总分20分  交期占用10分
						if(days>=1&&days<=7){
							//扣除3分
							JQ_GRADE = SsessEntity.JQ_GRADE -3;
						}
						if(days>7&&days<=14){
							//扣除5分
							JQ_GRADE = SsessEntity.JQ_GRADE -5;
						}
						if(days>15){
							//扣除10分
							JQ_GRADE = SsessEntity.JQ_GRADE -10;
						}
						if(days<=0){
							//不扣分
							JQ_GRADE = SsessEntity.JQ_GRADE -0;
							JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE;
						}
						if(days>0){
							//交货量占10分
							if(maapplyQty.intValue()!=0){
								//mashippedQty 收货数量
								//maapplyQty 订单申请数量
								BigDecimal div = mashippedQty.divide(maapplyQty,10,BigDecimal.ROUND_HALF_DOWN);//交货比例
								//四舍五入法
								div = div.setScale(4, BigDecimal.ROUND_HALF_UP);
								DecimalFormat df = new DecimalFormat("0.00%");
								//System.out.println("订单数量："+mashippedQty+"收货数量"+mashippedQty);
						        //按照交货比例算出对应的分数
						        JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE * Double.parseDouble(df.format(div).substring(0, df.format(div).length()-1))/100;
						        //价格的合理性
						        BigDecimal taxPrice = nproPurchRequestItemEntity.getTaxPrice(); //供应商供货的价格
						        
						        //得到最低价格(物料号  SRM供应商编码  工厂)
						        EssentialInformationEntity essentialInformationEntity = this.essentialInformationServiceImpl.findByNo(nproPurchRequestItemEntity.getVendorCode(),
						        		nproPurchRequestItemEntity.getFactory(), nproPurchRequestItemEntity.getMaterialCode(), null);
						        //当前价格减去最低价格
						        BigDecimal bignum = (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(essentialInformationEntity.getPrice());  //最低价格(后期要从信息记录维护进去的)
						        //差价比率
						        BigDecimal bignum1 =  (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(bignum).divide(new BigDecimal(5),10,BigDecimal.ROUND_HALF_DOWN);
						        //截取
						        flat = Double.parseDouble(df.format(bignum1).substring(0, df.format(div).length()-2));
							}
						}
				        if(flat>0&&flat<=2){
				        	JG_GRADE = SsessEntity.JG_GRADE -1;
				        }
				        if(flat>=3&&flat<=4){
				        	JG_GRADE = SsessEntity.JG_GRADE -2;
				        }
				        if(flat>=5&&flat<=6){
				        	JG_GRADE = SsessEntity.JG_GRADE -3;
				        }
				        if(flat>=7&&flat<=8){
				        	JG_GRADE = SsessEntity.JG_GRADE -4;
				        }
				        if(flat>=9&&flat<=10){
				        	JG_GRADE = SsessEntity.JG_GRADE -5;
				        }
				        if(flat>=11&&flat<=12){
				        	JG_GRADE = SsessEntity.JG_GRADE -6;
				        }
				        if(flat>=13&&flat<=14){
				        	JG_GRADE = SsessEntity.JG_GRADE -7;
				        }
				        if(flat>=15&&flat<=16){
				        	JG_GRADE = SsessEntity.JG_GRADE -8;
				        }
				        if(flat>=17&&flat<=18){
				        	JG_GRADE = SsessEntity.JG_GRADE -9;
				        }
				        if(flat>=19){
				        	JG_GRADE = SsessEntity.JG_GRADE -10;
				        }
			            if(flat<=0){
			            	JG_GRADE =SsessEntity.JG_GRADE;
			            }
			            //设置关于交货的的评分
			            Date dat = new Date();
			            ssessEntity.setMarkId(nproPurchRequestItemEntity.getPkId());//订单行项目
			            ssessEntity.setPoNo(nproPurchRequestItemEntity.getPurchRequestCode()); //申请编码
				        ssessEntity.setDeliveryTimeMark(JQ_GRADE+JH_NUM_GRADE); //交货评分
				        ssessEntity.setPriceMark(JG_GRADE);//价格分数
				        ssessEntity.setCreaDate(dat); //创建时间
				        ssessEntity.setMaterialCode(nproPurchRequestItemEntity.getMaterialCode());//物料编码
				        ssessEntity.setWerks(nproPurchRequestItemEntity.getFactory()); //工厂
				        ssessEntity.setVendorCode(nproPurchRequestItemEntity.getVendorCode());//供应商编码
				        ssessEntity.setVendorName(nproPurchRequestItemEntity.getVendorName()); //供应商名称
				        ssessEntity.setPoFlag("NPRO"); //非生产 NPRO  生产 PRO
				        ssessEntity.setDeliveryDate(nproPurchRequestItemEntity.getDeliveryDate()); //交货日期
				        ssessService.insertOrUpdate(ssessEntity);
				        //每循环一次分数重置0
				        JQ_GRADE = 0;
				        JH_NUM_GRADE = 0;
				        JG_GRADE = 0;
					}
					
		}
	/**
	 * 新增道月度评分表里面
	 */
	@Scheduled(cron = "0/45 * * * * ?")
	public void PROAScore() {
		 //这里的定时任务的代码执行块
		double JQ_GRADE = 0; //交期最终得分；
		double JH_NUM_GRADE = 0; //交货数量
		double JG_GRADE = 0; //价格分数
		SsessEntity ssessEntity = new SsessEntity();
		List<SsessAndProPurchRequest> list = this.proPurchRequestItemService.selectSsessSimplify();
		 // g.vendor_name AS vendorName,  a.srm_code AS srmCode, b.werks, d.po_no, a.material_code
		for(SsessAndProPurchRequest ssessAndProPurchRequest : list){
			 SsessEntity SsessEntity = this.ssessService.findBySesss(ssessAndProPurchRequest.getSapCode(), 
	            		ssessAndProPurchRequest.getWerks(), ssessAndProPurchRequest.getMaterialCode());
	            if(SsessEntity==null||"".equals(SsessEntity)){
	            	    ssessEntity.setMarkId(SunnykidText.getUUID());//订单行项目
			            ssessEntity.setDeliveryTimeMark(JQ_GRADE+JH_NUM_GRADE); //交货评分
				        ssessEntity.setPriceMark(JG_GRADE);//价格分数
				        ssessEntity.setCreaDate(DateUtils.currentDateTime()); //创建时间
				        ssessEntity.setMaterialCode(ssessAndProPurchRequest.getMaterialCode());//物料编码
				        ssessEntity.setWerks(ssessAndProPurchRequest.getWerks()); //工厂
				        ssessEntity.setVendorCode(ssessAndProPurchRequest.getSapCode());//供应商编码
				       
				        ssessEntity.setVendorName(ssessAndProPurchRequest.getVendorName()); //供应商名称
				        ssessEntity.setPoFlag("PRO"); //非生产 NPRO  生产 PRO
				        this.ssessService.insert(ssessEntity);
	            }
		}
				
	}
	/**
	 * 计算评估分数 (生产进行评分)
	 */ 
	@Scheduled(cron = "0/5 * * * * ?")
	public void addPROAScore() {
		        //这里的定时任务的代码执行块
				double JQ_GRADE = 0; //交期最终得分；
				double JH_NUM_GRADE = 0; //交货数量
				double JG_GRADE = 0; //价格分数
				/**************需要修改**2019-05-30********************/
				List<SsessAndProPurchRequest> list = this.proPurchRequestItemService.selectSsess();
				String purchRequestCodetem =""; //采购申请临时编码
				int applyQty=0;        //订单数量
				 // 不同类型转为BigDecimal
				BigDecimal maapplyQty = new BigDecimal(applyQty);
				SsessEntity ssessEntity = new SsessEntity();
				double flat = 0;
			if(list!=null&&list.size()>0){
				for(SsessAndProPurchRequest ssessAndProPurchRequest : list){
					Date date = ssessAndProPurchRequest.getDeliveryDate(); //得到交货日期
					Date stockTime = ssessAndProPurchRequest.getStockTime(); //收货日期
					String purchRequestCode =ssessAndProPurchRequest.getPurchRequestCode(); //采购申请编码
					//获取订单号
					String poNo = ssessAndProPurchRequest.getPoNo();
					int shippedQty = 0;
					BigDecimal mashippedQty = new BigDecimal(shippedQty);
					//根据订单号查询
					List<StockEntity> listStock = stockService.selectStockByDdh(poNo); //获取期内实际交货量
					if(listStock.size()>0){
						for(StockEntity stock : listStock){
							//收货数量(有分批发货数量)
							mashippedQty = mashippedQty.add(stock.getShippedQty());
						}
					    if(null!=purchRequestCodetem&&!purchRequestCodetem.equals(purchRequestCode)){
								purchRequestCodetem = purchRequestCode;
						}
						//防止出现空指针问题
						if(null!=purchRequestCodetem&&purchRequestCodetem.equals(purchRequestCode)){
							maapplyQty = ssessAndProPurchRequest.getApplyQty();
						}
					}
					java.util.Date deliveryDate = DateUtils.stringToDate(DateUtils.format(date, DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN);	//格式货交货日期
					java.util.Date newDate = DateUtils.stringToDate(DateUtils.format(DateUtils.currentDateTime(), DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN); //系统时间格式话
					int days=0;
					if(null!=deliveryDate){
						if(stockTime!=null){
							days =  DateUtils.differentDaysByMillisecond(stockTime, deliveryDate); //交期减去系统日期
						}
						days =  DateUtils.differentDaysByMillisecond(newDate, deliveryDate); //交期减去系统日期
					}
					//得到交货日期和系统时间相差几天
					//总分20分  交期占用10分
					if(days>=1&&days<=7){
						//扣除3分
						JQ_GRADE = SsessEntity.JQ_GRADE -3;
					}
					if(days>7&&days<=14){
						//扣除5分
						JQ_GRADE = SsessEntity.JQ_GRADE -5;
					}
					if(days>15){
						//扣除10分
						JQ_GRADE = SsessEntity.JQ_GRADE -10;
					}
					if(days<=0){
						//不扣分
						JQ_GRADE = SsessEntity.JQ_GRADE -0;
						JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE;
					}
					if(days>0){
						//交货量占10分
						if(maapplyQty.intValue()!=0){
							//mashippedQty 收货数量
							//maapplyQty 订单申请数量
							BigDecimal div = mashippedQty.divide(maapplyQty,10,BigDecimal.ROUND_HALF_DOWN);//交货比例
							//四舍五入法
							div = div.setScale(4, BigDecimal.ROUND_HALF_UP);
							DecimalFormat df = new DecimalFormat("0.00%");
							System.out.println("交货率："+div);
							System.out.println("订单数量："+mashippedQty+"收货数量"+mashippedQty);
					        //按照交货比例算出对应的分数
					        JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE * Double.parseDouble(df.format(div).substring(0, df.format(div).length()-1))/100;
					        //价格的合理性
					        BigDecimal taxPrice = ssessAndProPurchRequest.getTaxPrice(); //供应商供货的价格
					        //得到最低价格(物料号  SRM供应商编码  工厂)
					        EssentialInformationEntity essentialInformationEntity = 
					        		this.essentialInformationServiceImpl.findByNo(ssessAndProPurchRequest.getSapCode(),
					        				ssessAndProPurchRequest.getWerks(), ssessAndProPurchRequest.getMaterialCode(), null);
					        //当前价格减去最低价格
					        BigDecimal bignum = (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(essentialInformationEntity.getPrice());  //最低价格(后期要从信息记录维护进去的)
					        //差价比率
					        BigDecimal bignum1 =  (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(bignum).divide(new BigDecimal(5),10,BigDecimal.ROUND_HALF_DOWN);
					        //截取
					        flat = Double.parseDouble(df.format(bignum1).substring(0, df.format(div).length()-2));
						}
					}
			        if(flat>0&&flat<=2){
			        	JG_GRADE = SsessEntity.JG_GRADE -1;
			        }
			        if(flat>=3&&flat<=4){
			        	JG_GRADE = SsessEntity.JG_GRADE -2;
			        }
			        if(flat>=5&&flat<=6){
			        	JG_GRADE = SsessEntity.JG_GRADE -3;
			        }
			        if(flat>=7&&flat<=8){
			        	JG_GRADE = SsessEntity.JG_GRADE -4;
			        }
			        if(flat>=9&&flat<=10){
			        	JG_GRADE = SsessEntity.JG_GRADE -5;
			        }
			        if(flat>=11&&flat<=12){
			        	JG_GRADE = SsessEntity.JG_GRADE -6;
			        }
			        if(flat>=13&&flat<=14){
			        	JG_GRADE = SsessEntity.JG_GRADE -7;
			        }
			        if(flat>=15&&flat<=16){
			        	JG_GRADE = SsessEntity.JG_GRADE -8;
			        }
			        if(flat>=17&&flat<=18){
			        	JG_GRADE = SsessEntity.JG_GRADE -9;
			        }
			        if(flat>=19){
			        	JG_GRADE = SsessEntity.JG_GRADE -10;
			        }
		            if(flat<=0){
		            	JG_GRADE =SsessEntity.JG_GRADE;
		            }
		            //设置关于交货的的评分
		            Date dat = new Date();
		            
		            /*******************数据重复**************************/
		            SsessEntity SsessEntity = this.ssessService.findBySesss(ssessAndProPurchRequest.getSapCode(), 
		            		ssessAndProPurchRequest.getWerks(), ssessAndProPurchRequest.getMaterialCode());
		            
		            /*****************2019-06-15 修改***************************/
		            if(SsessEntity!=null||!"".equals(SsessEntity)){
			            ssessEntity.setPoNo(ssessAndProPurchRequest.getPurchRequestCode()); //申请编码
				        ssessEntity.setDeliveryTimeMark(JQ_GRADE+JH_NUM_GRADE); //交货评分
				        ssessEntity.setPriceMark(JG_GRADE);//价格分数
				        ssessEntity.setCreaDate(dat); //创建时间
				        ssessEntity.setMaterialCode(ssessAndProPurchRequest.getMaterialCode());//物料编码
				        ssessEntity.setWerks(ssessAndProPurchRequest.getWerks()); //工厂
				        ssessEntity.setVendorCode(ssessAndProPurchRequest.getSrmCode());//供应商编码
				        ssessEntity.setVendorName(ssessAndProPurchRequest.getVendorName()); //供应商名称
				        ssessEntity.setPoFlag("PRO"); //非生产 NPRO  生产 PRO
				        ssessEntity.setDeliveryDate(ssessAndProPurchRequest.getDeliveryDate()); //交期
				        StockEntity sto = stockService.selectById(ssessAndProPurchRequest.getPkId());
				        if(sto!=null){
				        	ssessEntity.setStockTime(sto.getStockTime());//入库时间
				        }
				        this.ssessService.updateById(ssessEntity);
				    
	            }
			        //每循环一次分数重置0
			        JQ_GRADE = 0;
			        JH_NUM_GRADE = 0;
			        JG_GRADE = 0;
				}
			}
				/****************需求修改*************************/
				/*List<SsessAndProPurchRequest> list = this.proPurchRequestItemService.selectSsess();
				String purchRequestCodetem =""; //采购申请临时编码
				int applyQty=0;        //订单数量
				 // 不同类型转为BigDecimal
				BigDecimal maapplyQty = new BigDecimal(applyQty);
				SsessEntity ssessEntity = new SsessEntity();
				double flat = 0;
				for(SsessAndProPurchRequest ssessAndProPurchRequest : list){
					Date date = ssessAndProPurchRequest.getDeliveryDate(); //得到交货日期
					Date stockTime = ssessAndProPurchRequest.getStockTime(); //收货日期
					String purchRequestCode =ssessAndProPurchRequest.getPurchRequestCode(); //采购申请编码
					//获取订单号
					String poNo = ssessAndProPurchRequest.getPoNo();
					int shippedQty = 0;
					BigDecimal mashippedQty = new BigDecimal(shippedQty);
					//根据订单号查询
					List<StockEntity> listStock = stockService.selectStockByDdh(poNo); //获取期内实际交货量
					if(listStock.size()>0){
						for(StockEntity stock : listStock){
							//收货数量(有分批发货数量)
							mashippedQty = mashippedQty.add(stock.getShippedQty());
						}
					    if(null!=purchRequestCodetem&&!purchRequestCodetem.equals(purchRequestCode)){
								purchRequestCodetem = purchRequestCode;
						}
						//防止出现空指针问题
						if(null!=purchRequestCodetem&&purchRequestCodetem.equals(purchRequestCode)){
							maapplyQty = ssessAndProPurchRequest.getApplyQty();
							//maapplyQty = maapplyQty.add(ssessAndProPurchRequest.getApplyQty());//订单申请总数量
						}
					}
					java.util.Date deliveryDate = DateUtils.stringToDate(DateUtils.format(date, DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN);	//格式货交货日期
					java.util.Date newDate = DateUtils.stringToDate(DateUtils.format(DateUtils.currentDateTime(), DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN); //系统时间格式话
					int days=0;
					if(null!=deliveryDate){
						if(stockTime!=null){
							days =  DateUtils.differentDaysByMillisecond(stockTime, deliveryDate); //交期减去系统日期
						}
						days =  DateUtils.differentDaysByMillisecond(newDate, deliveryDate); //交期减去系统日期
					}
					//得到交货日期和系统时间相差几天
					//总分20分  交期占用10分
					if(days>=1&&days<=7){
						//扣除3分
						JQ_GRADE = SsessEntity.JQ_GRADE -3;
					}
					if(days>7&&days<=14){
						//扣除5分
						JQ_GRADE = SsessEntity.JQ_GRADE -5;
					}
					if(days>15){
						//扣除10分
						JQ_GRADE = SsessEntity.JQ_GRADE -10;
					}
					if(days<=0){
						//不扣分
						JQ_GRADE = SsessEntity.JQ_GRADE -0;
						JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE;
					}
					if(days>0){
						//交货量占10分
						if(maapplyQty.intValue()!=0){
							//mashippedQty 收货数量
							//maapplyQty 订单申请数量
							BigDecimal div = mashippedQty.divide(maapplyQty,10,BigDecimal.ROUND_HALF_DOWN);//交货比例
							//四舍五入法
							div = div.setScale(4, BigDecimal.ROUND_HALF_UP);
							DecimalFormat df = new DecimalFormat("0.00%");
							System.out.println("交货率："+div);
							System.out.println("订单数量："+mashippedQty+"收货数量"+mashippedQty);
					        //按照交货比例算出对应的分数
					        JH_NUM_GRADE = SsessEntity.JH_NUM_GRADE * Double.parseDouble(df.format(div).substring(0, df.format(div).length()-1))/100;
					        //价格的合理性
					        BigDecimal taxPrice = ssessAndProPurchRequest.getTaxPrice(); //供应商供货的价格
					        
					        //得到最低价格(物料号  SRM供应商编码  工厂)
					        EssentialInformationEntity essentialInformationEntity = 
					        		this.essentialInformationServiceImpl.findByNo(ssessAndProPurchRequest.getSapCode(),
					        				ssessAndProPurchRequest.getFactory(), ssessAndProPurchRequest.getMaterialCode());
					        //当前价格减去最低价格
					        BigDecimal bignum = (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(essentialInformationEntity.getPrice());  //最低价格(后期要从信息记录维护进去的)
					        //差价比率
					        BigDecimal bignum1 =  (taxPrice==null)?new BigDecimal(0):taxPrice.subtract(bignum).divide(new BigDecimal(5),10,BigDecimal.ROUND_HALF_DOWN);
					        //截取
					        flat = Double.parseDouble(df.format(bignum1).substring(0, df.format(div).length()-2));
						}
					}
			        if(flat>0&&flat<=2){
			        	JG_GRADE = SsessEntity.JG_GRADE -1;
			        }
			        if(flat>=3&&flat<=4){
			        	JG_GRADE = SsessEntity.JG_GRADE -2;
			        }
			        if(flat>=5&&flat<=6){
			        	JG_GRADE = SsessEntity.JG_GRADE -3;
			        }
			        if(flat>=7&&flat<=8){
			        	JG_GRADE = SsessEntity.JG_GRADE -4;
			        }
			        if(flat>=9&&flat<=10){
			        	JG_GRADE = SsessEntity.JG_GRADE -5;
			        }
			        if(flat>=11&&flat<=12){
			        	JG_GRADE = SsessEntity.JG_GRADE -6;
			        }
			        if(flat>=13&&flat<=14){
			        	JG_GRADE = SsessEntity.JG_GRADE -7;
			        }
			        if(flat>=15&&flat<=16){
			        	JG_GRADE = SsessEntity.JG_GRADE -8;
			        }
			        if(flat>=17&&flat<=18){
			        	JG_GRADE = SsessEntity.JG_GRADE -9;
			        }
			        if(flat>=19){
			        	JG_GRADE = SsessEntity.JG_GRADE -10;
			        }
		            if(flat<=0){
		            	JG_GRADE =SsessEntity.JG_GRADE;
		            }
		            //设置关于交货的的评分
		            Date dat = new Date();
		            ssessEntity.setMarkId(ssessAndProPurchRequest.getPkId());//订单行项目
		            ssessEntity.setPoNo(ssessAndProPurchRequest.getPurchRequestCode()); //申请编码
			        ssessEntity.setDeliveryTimeMark(JQ_GRADE+JH_NUM_GRADE); //交货评分
			        ssessEntity.setPriceMark(JG_GRADE);//价格分数
			        ssessEntity.setCreaDate(dat); //创建时间
			        ssessEntity.setMaterialCode(ssessAndProPurchRequest.getMaterialCode());//物料编码
			        ssessEntity.setWerks(ssessAndProPurchRequest.getFactory()); //工厂
			        ssessEntity.setVendorCode(ssessAndProPurchRequest.getSapCode());//供应商编码
			        ssessEntity.setVendorName(ssessAndProPurchRequest.getVendorName()); //供应商名称
			        ssessEntity.setPoFlag("PRO"); //非生产 NPRO  生产 PRO
			        ssessEntity.setDeliveryDate(ssessAndProPurchRequest.getDeliveryDate()); //交期
			        
			        StockEntity sto = stockService.selectById(ssessAndProPurchRequest.getPkId());
			        if(sto!=null){
			        	ssessEntity.setStockTime(DateUtils.stringToDate(sto.getStockTime(), DateUtils.DATE_PATTERN));//入库时间
			        }
			        ssessService.insertOrUpdate(ssessEntity);
			        //每循环一次分数重置0
			        JQ_GRADE = 0;
			        JH_NUM_GRADE = 0;
			        JG_GRADE = 0;
				}*/
				}

				/**************需要修改**********************/
}
